package injections.example;

import com.google.common.io.ByteStreams;
import injections.Injector;
import injections.JSONSource;
import injections.annotations.PostInject;
import injections.annotations.Property;

import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by rehak on 2/10/14.
 */
public class MyClass {

    private final int[] ar2;
    private final String txt;
    private final NumService ns2;
    private final String txt2;
    @Property
    private File testFile;
    @Property
    private CustomConverterClass custom;
    @Property
    public NumService numService;
    @Property
    public NumService anonNumService;
    @Property
    public int[] ar;
    @Property
    public Set<Integer> col = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());

    public MyClass(@Property int[] ar2,
                   @Property String txt,
                   @Property String txt2,
                   @Property NumService ns2) {
        this.ar2 = ar2;
        this.txt = txt;
        this.ns2 = ns2;
        this.txt2 = txt2;
    }

    @PostInject
    private void postInject() {
        // hello I was injected!
        System.out.println("My num is " + numService.getNum());
        System.out.println("My anon num is " + anonNumService.getNum());
    }

    public static void main(String[] args) throws Exception {
        InputStream in = MyClass.class.getResourceAsStream("/example.json");
        String j = new String(ByteStreams.toByteArray(in), "UTF-8");
        in.close();
        JSONSource jsonSource = new JSONSource(j);
        Injector injector = new Injector(jsonSource.get());
        // custom converter
        injector.addConverter(new CustomConverter());
        // inject
        MyClass injected = (MyClass) injector.injectRoot();
        int n1 = injected.numService.getNum();
        int n2 = injected.anonNumService.getNum();
        System.out.println("numService: " + n1 + ", anonNumService: " + n2 + ", array: " + Arrays.toString(injected.ar) + ", col: " + injected.col);
    }


}
